package class_wuziqi;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import javax.swing.JOptionPane;

public class ReceiveThread implements Runnable{
	private Socket socket;
	private String oponentName;
	public ReceiveThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		while(true){
			try {
				ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
				Object o=ois.readObject();
				if(o instanceof User){
					User u=(User)o;
					Login.getInstance().ipAddress_text.setText(u.getIp());
					Login.getInstance().ipAddress_text.setEnabled(false);
					MainPro.getInstance().setUser(u, false);
					oponentName=u.getName();
				}
				else if(o instanceof Integer){
					int code=(int)o;
					switch (code) {
					case MainPro.GAMESTART:
						ChessArray.getInstance().clear();
						ChessBoard.getInstance().repaint();
						int f=JOptionPane.showConfirmDialog(null, "对方请求开始，是否同意？", "邀请", JOptionPane.YES_NO_OPTION);
						if(f==0){
							MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.AGREE_START);
							MainPro.getInstance().isStart=true;
							MainPro.getInstance().chessColor=Color.BLACK;
							MainPro.getInstance().isMy=true;
							MainPro.getInstance().isOver=false;
							MainPro.getInstance().Win=false;
							ChessBoard.getInstance().repaint();
							Left_Panel.getInstance().setStart();
						}
						else{
							MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.DISAGREE_START);
						}
						break;
					case MainPro.AGREE_START:
						ChessArray.getInstance().clear();
						ChessBoard.getInstance().repaint();
						MainPro.getInstance().isStart=true;
						MainPro.getInstance().isOver=false;
						MainPro.getInstance().Win=false;
						MainPro.getInstance().isMy=false;
						MainPro.getInstance().chessColor=Color.WHITE;
						Left_Panel.getInstance().setStart();
						ChessArray.getInstance().chessList.clear();
						break;
					case MainPro.DEFEAT:
						int defeat=JOptionPane.showConfirmDialog(null, "对方认输，是否同意？", "消息", JOptionPane.YES_NO_OPTION);
						if(defeat==0){
							MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.AGREE_DEFEAT);
							ChessArray.getInstance().clear();
							ChessBoard.getInstance().repaint();
							MainPro.getInstance().isStart=false;
							MainPro.getInstance().isOver=false;
							MainPro.getInstance().Win=false;
							Left_Panel.getInstance().setOver();
						}else{
							MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.DISAGREE_DEFEAT);
						}
						break;
					case MainPro.AGREE_DEFEAT:
						ChessArray.getInstance().clear();
						ChessBoard.getInstance().repaint();
						MainPro.getInstance().isStart=false;
						MainPro.getInstance().isOver=false;
						MainPro.getInstance().Win=false;
						Left_Panel.getInstance().setOver();
						break;
					case MainPro.REGRET:
						int regret=JOptionPane.showConfirmDialog(null, "对方请求悔棋一步，是否同意？", "消息", JOptionPane.YES_NO_OPTION);
						if(regret==0){
							MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.AGREE_REGRET);
							ChessArray.getInstance().remove();
							ChessBoard.getInstance().repaint();
						}
						break;
					case MainPro.AGREE_REGRET:
						ChessArray.getInstance().remove();
						ChessBoard.getInstance().repaint();	
						break;
					case MainPro.DRAW:
						int draw=JOptionPane.showConfirmDialog(null, "对方请求和棋，是否同意？", "请求", JOptionPane.YES_NO_OPTION);
						if(draw==0){
							JOptionPane.showMessageDialog(null, "平局");
							MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.AGREE_DRAW);
							MainPro.getInstance().isStart=false;
							MainPro.getInstance().isOver=false;
							MainPro.getInstance().Win=false;
							Left_Panel.getInstance().setOver();
							ChessArray.getInstance().clear();
							ChessBoard.getInstance().repaint();
						}
						else{
							MainPro.getInstance().sendMessage(Login.getInstance().getSocket(), MainPro.DISAGREE_DRAW);
						}
						break;
					case MainPro.AGREE_DRAW:
						JOptionPane.showMessageDialog(null, "平局");
						MainPro.getInstance().isStart=false;
						MainPro.getInstance().isOver=false;
						MainPro.getInstance().Win=false;
						Left_Panel.getInstance().setOver();
						ChessArray.getInstance().clear();
						ChessBoard.getInstance().repaint();
						break;
					case MainPro.isWin:
						MainPro.getInstance().Win=true;
						MainPro.getInstance().isStart=false;
						Left_Panel.getInstance().setOver();
						break;
					default:JOptionPane.showMessageDialog(null, "对方拒绝请求");break;
					}
				}
				else if(o instanceof Chess){
					ChessArray ca=ChessArray.getInstance();
					ca.setChess((Chess)o);
					MainPro.getInstance().isMy=true; 
					ChessBoard.getInstance().repaint();
				}
				else if(o instanceof String){
					String message=(String)o;
					MainPro.getInstance().jta.append(oponentName+">>"+message+"\n");
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
